package com.qianfeng.user.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qianfeng.user.bean.User;
import com.qianfeng.user.service.UserService;
import com.qianfeng.utils.Response;

@Controller
@RequestMapping("/user/")
public class UserController {
	UserService userService=new UserService();
	
	@RequestMapping("tomain")
	public String toMain() {
			return "main";
		
	}
	@RequestMapping("tologin")
	public String toLogin() {
		return "login";
	}
	
	@ResponseBody
	@RequestMapping("login")
	public Response Login(User user ,HttpSession session) {
		Response rs=new Response();
		if(user.getUserName_()==null||"".equals(user.getUserName_().trim())) {  
			rs.setCode(-1);
			rs.setMessage("用户名不能为空");
			return rs;
		}
		
		if(user.getPassword_()==null||"".equals(user.getPassword_().trim())) {
			rs.setCode(-1);
			rs.setMessage("密码不能为空");
			return rs;
		}
		
		User u=userService.getUserByUserNameAndPassword(user);
		
		if(u!=null) {
			
			session.setAttribute("user", u);
			rs.setCode(1);
			rs.setMessage("登陆成功");
			
		}else {
			rs.setCode(-1);
			rs.setMessage("登陆失败");
			
		}
		return rs;
	}
	@RequestMapping("logOut")
	public String LogOut(HttpSession session) {
		session.removeAttribute("user");
		return "redirect:/user/tomain";
	}
}
